package com.ftg.midamall.admin.mapper;

import com.ftg.midamall.admin.dto.UmsRoleDto;
import com.ftg.midamall.admin.entity.UmsRole;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 Mapper 接口
 * </p>
 *
 * @author qjw
 * @since 2023-10-25
 */
@Mapper
public interface UmsRoleMapper extends BaseMapper<UmsRole> {
    /**
     * 添加角色
     *
     * @param ur
     * @return
     */
    int insertRole(@Param("ur") UmsRoleDto ur);



    /**
     * 根据name查询是否存在
     *
     * @param name
     * @return
     */
    UmsRoleDto selectByName(@Param("name") String name, @Param("id") Long id);

    @Select("select * from ums_role where name =#{name}")
    UmsRoleDto selectRoleName(@Param("name")String name);

    /**
     * 根据 id 更改信息
     * @param id
     * @return
     */
    int updateById(@Param("ur") UmsRoleDto ur ,@Param("id") Long id);

//
//    /**
//     * 根据admin_id查询角色role_id
//     * @param admin_id
//     * @return
//     */
//    @Select("select role_id from ums_admin_role_relation where admin_id=#{admin_id}")
//    UmsRole selectById(@Param("admin_id") Long admin_id);
//


    /**
     *人数+1
     */
    @Update("update ums_role set admin_count = admin_count+1 where id in(select role_id from ums_admin_role_relation where admin_id=#{admin_id}) ")
    int updateAdminCount(@Param("id") Long id,@Param("admin_id") Long admin_id);

    /**
     *人数-1
     */
    @Update("update ums_role set admin_count = admin_count-1 where id in(select role_id from ums_admin_role_relation where admin_id=#{admin_id}) ")
    int reduceCount(@Param("id") Long id,@Param("admin_id") Long admin_id);

    /**
     * 根据id删除角色
     * @param id
     * @return
     */
    int deleteById(@Param("id") Long id);

    UmsRole selectByIdCount(Long id);
    /**
     * 切换状态
     * @param id 主键
     * @return 成功 失败
     */
    @Update("update ums_role set status = if(status=0,1,0) where id = #{id}")
    Boolean switchStatus(Long id);


//    @Select("select admin_count from ums_role where id =#{id}")
//    int count(@Param("id") int id);

    @Select("select id from ums_role where name =#{name}")
    Long selectId(@Param("name") String name);



}
